package com.cedarhd.brzx.controller;

import com.cedarhd.brzx.common.JsonResult;
import com.cedarhd.brzx.common.enums.ResultCode;
import com.cedarhd.brzx.common.utils.PoolUtil;
import com.cedarhd.brzx.common.utils.ValidatorUtil;
import com.cedarhd.brzx.domain.service.FileService;
import com.cedarhd.brzx.domain.service.SoaMemberService;
import com.cedarhd.brzx.dto.request.EnterpriseInformationDTO;
import com.cedarhd.brzx.dto.request.VerifyResultDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutorService;

/**
 *
 * Created by Administrator on 2018/5/3.
 */
@RestController
@Api(value = "通联支付企业相关接口",description = "通联支付企业相关接口")
public class SoaMemberCompanyController {

    private static Logger log = LoggerFactory.getLogger(SoaMemberCompanyController.class);

    @Autowired
    private SoaMemberService soaMemberService;

    @Autowired
    private FileService fileService;

    @RequestMapping(value = Routes.SOA_MEMBER_ENTERPRISE_INFORMATION, method = RequestMethod.POST)
    @ApiOperation(value = "设置企业信息", notes = "设置企业信息", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public JsonResult enterpriseInformation(@RequestBody EnterpriseInformationDTO req) {
        log.info("enterpriseInformation params = {}", req);
        ValidatorUtil.validate(req);
        JSONObject response = soaMemberService.enterpriseInformation(req);
        ValidatorUtil.checkJsonResp(response);
        String resultJson = response.getString("signedValue");
        return JsonResult.renderMsg(new JSONObject(resultJson).toMap());
    }

    @RequestMapping(value = Routes.SOA_MEMBER_ENTERPRISE_VERIFY_RESULT, method = RequestMethod.POST)
    @ApiOperation(value = "企业信息审核结果通知", notes = "企业信息审核结果通知", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public JsonResult verifyResult(@RequestBody VerifyResultDTO req){
        log.info("verifyResult params {}",req);
        JSONObject result = soaMemberService.verifyResult(req);
        try {
            ExecutorService executor = PoolUtil.getPoolExecutor();
            Runnable task = ()-> fileService.writeVerifyResult(result.toString());
            executor.execute(task);
        }catch (Exception e){
            log.info("writeVerifyResult error!",e);
        }

        return JsonResult.renderMsg(ResultCode.SUCC);
    }

}
